################################################################################
# Replication script for figures 1 and 2
# The populist radical right as memory entrepreneur? The prominence, sentiment, and interpretations of history in the German parliament
# British Journal of Political Science
################################################################################
rm(list = ls())

setwd("C:/Users/fkrawatzek/Dropbox/AfD/Writings/2022 National analysis/BJPS submission/BJPS_revision/dataverse")

pacman::p_load(openxlsx, ggplot2)

stand_theme <- theme(plot.title=element_text(hjust=0.5, size=20),
                     axis.title.y=element_text(vjust=1,size=16),
                     axis.text.y=element_text(size=16),
                     axis.text.x=element_text(size=16),
                     plot.subtitle=element_text(size=12),
                     plot.caption = element_text(),
                     legend.text=element_text(size=16),
                     legend.key.size=unit(3, 'lines'),
                     legend.title=element_blank())

parties6_colour <- c("#9ecae1","#a9d185", "#737373","#d39ee4","#fed976","#fa8a80")
parties6_blackwhite <- c("#000000","#3a3a3a","#838383", "#aeaeae","#c9c9c9","#e5e5e5")

plot_df <- read.xlsx("./Data Figures 1 + 2.xlsx", sheet = 1)

mean <- mean(plot_df$Memory1_Percentage)
sd <- sd(plot_df$Memory1_Percentage)
ggplot(plot_df, aes(x = party, y = Memory1_Percentage, fill = party)) +
  geom_hline(yintercept = 0)+
  geom_hline(aes(yintercept = mean), color="red")+
  geom_hline(aes(yintercept = mean-sd), color="red", linetype="dashed") + geom_hline(aes(yintercept = mean+sd), color="red", linetype="dashed")+
  geom_bar(stat = "identity", position = "dodge", alpha= 0.9) +
  labs(title = "Percentage of Extracts Containing at Least One Root Term - National Parliament",
       subtitle = paste0("For Root Terms in Group: Memory. The percentages reflect the ratio of 0 to >0 occurances per Party."),
       x ="",
       y = "Percentage of extracts containing one or more root terms",
       size = 16) +
  scale_fill_manual(values = parties6_colour) +
  #scale_fill_manual(values = parties6_blackwhite) +
  geom_text(data=plot_df[], aes(label=Memory1),
            position=position_dodge(0.9), vjust = -0.5, size = 6) +
  stand_theme + theme(legend.position = "none")
#filename <- "./Figure 1a Memory.png"
filename <- "./Figure 1a Memory-colour.png"
ggsave(file = filename, width = 12, height = 8, dpi=600)

mean <- mean(plot_df$Temporal1_Percentage)
sd <- sd(plot_df$Temporal1_Percentage)
ggplot(plot_df, aes(x = party, y = Temporal1_Percentage, fill = party)) +
  geom_hline(yintercept = 0)+
  geom_hline(aes(yintercept = mean), color="red")+
  geom_hline(aes(yintercept = mean-sd), color="red", linetype="dashed") + geom_hline(aes(yintercept = mean+sd), color="red", linetype="dashed")+
  geom_bar(stat = "identity", position = "dodge", alpha= 0.9) +
  labs(title = "Percentage of Extracts Containing at Least One Root Term - National Parliament",
       subtitle = paste0("For Root Terms in Group: Temporal. The percentages reflect the ratio of 0 to >0 occurances per Party."),
       x ="",
       y = "Percentage of extracts containing one or more root terms",
       size = 16) +
  scale_fill_manual(values = parties6_colour) +
  #scale_fill_manual(values = parties6_blackwhite) +
  geom_text(data=plot_df[], aes(label=Temporal1),
            position=position_dodge(0.9), vjust = -0.5, size = 6) +
  stand_theme + theme(legend.position = "none")
#filename <- "./Figure 1b Temporal.png"
filename <- "./Figure 1a Temporal-colour.png"
ggsave(file = filename, width = 12, height = 8, dpi=600)

mean <- mean(plot_df$RightWing1_Percentage)
sd <- sd(plot_df$RightWing1_Percentage)
ggplot(plot_df, aes(x = party, y = RightWing1_Percentage, fill = party)) +
  geom_hline(yintercept = 0)+
  geom_hline(aes(yintercept = mean), color="red")+
  geom_hline(aes(yintercept = mean-sd), color="red", linetype="dashed") + geom_hline(aes(yintercept = mean+sd), color="red", linetype="dashed")+
  geom_bar(stat = "identity", position = "dodge", alpha= 0.9) +
  labs(title = "Percentage of Extracts Containing at Least One Root Term - National Parliament",
       subtitle = paste0("For Root Terms in Group: RightWing. The percentages reflect the ratio of 0 to >0 occurances per Party."),
       x ="",
       y = "Percentage of extracts containing one or more root terms",
       size = 16) +
  scale_fill_manual(values = parties6_colour) +
  #scale_fill_manual(values = parties6_blackwhite) +
  geom_text(data=plot_df[], aes(label=RightWing1),
            position=position_dodge(0.9), vjust = -0.5, size = 6) +
  stand_theme + theme(legend.position = "none")
#filename <- "./Figure 2 RightWing.png"
filename <- "./Figure 1a RightWing-colour.png"
ggsave(file = filename, width = 12, height = 8, dpi=600)
